package com.zxy.supplier_system.server.entity

import jakarta.persistence.*
import java.io.Serializable

@Entity
class MobileAppVersion {

    @Embeddable
    class Version : Serializable {
        var majorVersion: Int = 0
        var minorVersion: Int = 0
        var patchVersion: Int = 0
        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            if (other !is Version) return false

            if (majorVersion != other.majorVersion) return false
            if (minorVersion != other.minorVersion) return false
            if (patchVersion != other.patchVersion) return false

            return true
        }

        override fun hashCode(): Int {
            var result = majorVersion
            result = 31 * result + minorVersion
            result = 31 * result + patchVersion
            return result
        }

        override fun toString(): String {
            return "$majorVersion.$minorVersion.$patchVersion"
        }

        companion object {
            fun fromVersionString(versionString: String): Version {
                val strings = versionString.split(".")
                return Version().apply {
                    majorVersion = strings[0].toInt()
                    minorVersion = strings[1].toInt()
                    patchVersion = strings[2].toInt()
                }
            }
        }


    }

    @EmbeddedId
    lateinit var version: Version

    @OneToMany(mappedBy = "mobileAppVersion", cascade = [CascadeType.ALL], orphanRemoval = true)
    var updateNotes: MutableList<MobileAppVersionUpgradeNote> = mutableListOf()

}